//
//  FancyItem.swift
//  Fancyswap
//
//  Created by Francois Gratton on 2015-03-01.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class FancyItem {
    
    var thumbnail : UIImage?    // defaults to nil
    var largeImage : UIImage?
    var brand: String = ""
    var id: Int = 0
    var image : String = ""     // name + ext
    var name: String = ""
    var size: String = ""
    var username: String = ""
    var value: Int = 0
    var imageFiles = [String]()
    var thumbs = [UIImage]()
    
    init(){
    }
    
    /// create using json
    init (dic:NSDictionary) {
        let sjson = JSON(dic)
        importData(sjson)
    }
    
    init (swiftyDic:JSON) {
        importData(swiftyDic)
    }

    func importData (swiftyDic:JSON) {
        for (key, value) in swiftyDic
        {
            switch (key).lowercaseString
            {
            case "brand":
                self.brand = value.stringValue
            case "id":
                self.id = value.intValue
            case "image":
                self.image = value.stringValue
            case "name":
                self.name = value.stringValue
            case "username":
                self.username = value.stringValue
            case "size":
                self.size = value.stringValue
            case "value":
                self.value = value.intValue
            case "images":
                for img in value.arrayValue
                {
                    self.imageFiles.append(img.stringValue)
                }
                
            default:
                println("Unknown element: \(key):\(value)")
            }
        }
    }
    
    func imageCount() -> Int {
        return imageFiles.count
    }
    
    func toString() -> String {
        return "id: \(id), name: \(name), brand: \(brand), value: \(value), size: \(size), image: \(image)"
    }

}
